package com.opal.config;

import com.opal.filter.LoginFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Configuration   //需要此注解把此类作为配置类
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean timeFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //登录过期拦截
        LoginFilter loginFilter= new LoginFilter();
        filterRegistrationBean.setFilter(loginFilter);
        List<String> urls = new ArrayList<>();

        urls.add("/*"); //拦截所有

        filterRegistrationBean.setUrlPatterns(urls);//配置过滤规则,
        return filterRegistrationBean;
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * @Description: 对文件的路径进行配置, 创建一个虚拟路径/Path/** ，即只要在< img src="/Path/picName.jpg" />便可以直接引用图片
         *这是图片的物理路径  "file:/+本地图片的地址"
         */
        //registry.addResourceHandler("/Path/**").addResourceLocations("file://192.168.2.7/");
        registry.addResourceHandler("/ph/**").addResourceLocations("file://192.168.2.6/");
        registry.addResourceHandler("/local/**").addResourceLocations("file:///");
        //映射本地文件夹的操作：file:///+绝对路径
        //registry.addResourceHandler("/ceshi1/**").addResourceLocations("file:///F:/hehe/");

        //映射服务器文件夹的操作：file://+ip地址+（不用盘符）根文件夹开始路径，对象URI其实可以省略的
        String path="file:///"+System.getProperty("user.dir");
        log.info("成本报告上传文件映射的路径是："+path);
        registry.addResourceHandler("/fileServer/image/**").addResourceLocations(path+"\\fileServer\\image\\");
        //临时服务器文件
        registry.addResourceHandler("/temp/**").addResourceLocations(path+"\\temp\\");
        //super.addResourceHandlers(registry);
    }

    //自定义参数绑定注册
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
    }
}
